package
{
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import id.core.Application;
	import id.core.TouchSprite;
	import gl.events.TouchEvent;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author FT_Milenkij
	 */
	public class FT_Media extends Application
	{
		
		private var nav_pad:TouchSprite = new TouchSprite();
		private var nav_background:TouchSprite = new TouchSprite();
		private var text:TextField = new TextField();
		
		public function FT_Media():void
		{
			load_textures();
			enter_positions();
			sys_support();
		}
		
		private function load_textures():void
		{
			var nav_background_ldr:Loader = new Loader;
			var nav_pad_ldr:Loader = new Loader;
			
			nav_background_ldr.load(new URLRequest("pic_media/nav_background.png"));
			nav_background.addChild(nav_background_ldr);
			
			nav_pad_ldr.load(new URLRequest("pic_media/nav_pad.png"));
			nav_pad_ldr.x = -15;
			nav_pad_ldr.y = -15;
			nav_pad.addChild(nav_pad_ldr);
		}
		
		private function enter_positions():void
		{
			this.addChild(nav_background);
			this.addChild(nav_pad);
			
			//add_listeners
			nav_background.addEventListener(TouchEvent.TOUCH_DOWN, nav_pad_td_handler);
			nav_background.addEventListener(TouchEvent.TOUCH_MOVE, nav_pad_td_handler);
		}
		
		private function sys_support():void
		{
			var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0xFF0000;
            format.size = 40;
            format.underline = true;
			
			text.defaultTextFormat = format;
			text.autoSize = TextFieldAutoSize.LEFT;
			text.x = 400;
			text.y = 150;
			this.addChild(text);
			
			
		}
		
		private function nav_pad_td_handler(e:TouchEvent):void
		{
			var rad:Number = Math.sqrt(((e.stageX - 150) * (e.stageX - 150)) + ((e.stageY - 150) * (e.stageY - 150)));
			var proport:Number = rad / 135;
			trace("x: ", e.stageX, " | y: ", e.stageY);
			trace("rad: ", rad);
			trace("pr: ", proport);
			nav_pad.x = ((e.stageX - 150) / proport) + 150;
			nav_pad.y = ((e.stageY - 150) / proport) + 150;
			var angle:int = Math.atan2(150 - nav_pad.x, nav_pad.y - 150) * (180 / Math.PI);
			
			if (angle < 0)
			{
				angle = angle +360; 
			}
			
			trace (angle);
			
			if (angle > 90 && angle < 120)
			{
				text.text = "1";
			}
			
			if (angle > 170 && angle < 190)
			{
				text.text = "2";
			}
			
			if (angle > 240 && angle < 260)
			{
				text.text = "3";
			}
			
			if (angle > 315 && angle < 330)
			{
				text.text = "4";
			}
			
			if (angle > 30 && angle < 45)
			{
				text.text = "5";
			}
		}
	
	}

}